package lc20240503;

import java.util.HashMap;
import java.util.Objects;

//290单词规律
public class WordRule {
    public static void main(String[] args) {
        //注意这种特殊情况。
        String pattern = "abba";
        String s = "dog dog dog dog";
        System.out.println("wordPattern(pattern,s) = " + wordPattern(pattern, s));
    }

    public static boolean wordPattern(String pattern, String s) {
        HashMap<Character, String> map = new HashMap<>();
        String[] str = s.split(" ");
        if (str.length != pattern.length()) {
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (!map.containsKey(c)) {
                if(map.containsValue(str[i]))
                    return false;
                map.put(c,  str[i]);
            } else if (!Objects.equals(map.get(c), str[i])) {
                return false;
            }
        }
        return true;
    }
}
